//
//  ST_BTLicenceController.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/22.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyUserDefaults
import SwiftyAttributes
//import Rswift
import RxSwift

class ST_BTLicenceController: UIViewController {

    
    let agreeObserver:PublishSubject<Bool> = PublishSubject()
    
    override init(nibName: String?, bundle: Bundle?){
        super.init(nibName: nibName, bundle: bundle)
        
        self.overCurrentContext = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(root_view_offset)
        }
        
        root_view.addSubview(licence_label)
        licence_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(23)
            make.top.equalToSuperview().offset(26)
            make.height.equalTo(30)
        }
        
        root_view.addSubview(licence_image_view)
        licence_image_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(69)
            make.height.equalTo(licence_image_view_height)
        }
        
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(licence_image_view.snp.bottom).offset(4)
            
            make.bottom.equalToSuperview().offset(-131-root_view_offset)
        }
        
        
        
        
        root_view.addSubview(agree_btn)
        root_view.addSubview(cancel_btn)
        
        
        
        cancel_btn.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.width.height.equalTo(agree_btn)
            
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-root_view_offset)
        }
        
        
        agree_btn.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.width.equalTo(345)
            make.height.equalTo(44)
            make.bottom.equalTo(cancel_btn.snp.top)
            
        }
        
        
        
        b_t_create_attribute_text()
    }
    
    
    func b_t_create_attribute_text(){
        
        let normalAttributes = [Attribute.font(Font.systemFont(ofSize: 13)),Attribute.textColor(UIColor.init(hexString: "#717171")!)]
        
        let richText = "请确保你已满 18 岁并认真阅读".withAttributes(normalAttributes)
        
        richText.append("《用户协议》".withAttributes([Attribute.font(Font.systemFont(ofSize: 13)),Attribute.textColor(UIColor.init(hex: 0xFEC207)!),Attribute.link(URL.init(string: PH.userProtocolURL)!)]))
        
        richText.append("、".withAttributes(normalAttributes))
        
        richText.append("《隐私政策》".withAttributes([Attribute.font(Font.systemFont(ofSize: 13)),Attribute.textColor(UIColor.init(hex: 0xFEC207)!),Attribute.link(URL.init(string: PH.privacyProtocolURL)!)]))
        
        if Defaults[.isPanda] == false{
            
            richText.append("、".withAttributes(normalAttributes))
            
            richText.append("《\(Defaults[.pandaBiuBiuStr])》".withAttributes([Attribute.font(Font.systemFont(ofSize: 13)),Attribute.textColor(UIColor.init(hex: 0xFEC207)!),Attribute.link(URL.init(string: PH.userShareProtocolURL)!)]))
        }
        
        
        
        richText.append("。点击同意表示你已阅读并同意全部条款。为保障你的账号安全及内容存储、请授权我们使用设备信息、存储权限。设备信息仅用于生成设备号。".withAttributes(normalAttributes))
        
        
        let paragraph = NSMutableParagraphStyle()
        
        paragraph.lineSpacing = 5
        
        richText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraph, range: NSRange.init(location: 0, length: richText.length))
        
        content_label.attributedText = richText
        
    }
    
    
//    MARK: - Properties
    private lazy var root_view_offset = 20.cgFloat
    
    lazy var root_view:UIView = {
        let control = UIView()
        control.cornerRadius = 20
        control.backgroundColor = Color.white
        return control
    }()
    
    
    lazy var licence_label:UILabel = {
        let control = UILabel.init()
        control.text = "欢迎来到伴心"
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    private lazy var licence_image_view_height:CGFloat = ScreenWidth/375.0*226
    
    fileprivate lazy var licence_image_view:UIImageView = {
        
        let v = UIImageView.init(image: ST_ImageUtils.imageName("licence_center_img"))
        //v.cornerRadius = 5
        return v
    }()
    
    
    lazy var content_label:UITextView = {
        let control = UITextView()
        control.delegate = self
        control.isEditable = false
        control.isScrollEnabled = false
        control.tintColor = UIColor.init(hex: 0xFEC207)!
        control.backgroundColor = UIColor.white
//        control.backgroundColor = UIColor.random
        return control
    }()
    
    lazy var agree_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 22
        control.setTitleForAllStates("同意")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        control.setTitleColor(UIColor.white, for: UIControl.State.normal)
        control.addTarget(self, action: #selector(agree_licence_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    lazy var cancel_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        let leftTitle = "退出"
        control.setTitleForAllStates(leftTitle)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        control.setTitleColor(UIColor.init(hexString: "#ACB4C0"), for: UIControl.State.normal)
        control.addTarget(self, action: #selector(cacnel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    //    MARK: - Event
    
    @objc func agree_licence_handle(){
        
        Defaults[.agreeLicense] = true
        
        agreeObserver.onNext(true)
        
        dismiss(animated: false)
        
    }
    
    @objc func cacnel_handle(){
        
        agreeObserver.onNext(false)
        
        dismiss(animated: false)
    }

}


extension ST_BTLicenceController:UITextViewDelegate{
    
    
    @available(iOS 10.0, *)
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        
        let vc = ST_BTWebViewController()
        
        vc.urlStr = URL.absoluteString
        
        
        switch URL.absoluteString {
        case PH.userProtocolURL:
            vc.titleStr = "用户协议"
        case PH.privacyProtocolURL:
            vc.titleStr = "隐私政策"
        case PH.userShareProtocolURL:
            vc.titleStr = Defaults[.pandaBiuBiuStr]
        default: break
        }
        
        GlobalPresent(toController: vc)
        
        return false
    }
    
}
